package com.ibm.csr.staf;

import java.util.List;
import java.util.Map;

import com.ibm.staf.STAFResult;

public class KickResult {

	public int rc = 0;
	public int outrc = 0;
	public int errrc = 0;
	public String stdout;
	public String stderr;
	private boolean success = false;

	/**
	 * 
	 * @param rc1
	 * @param rc2
	 * @param rc3
	 * @param stdout
	 * @param stderr
	 */
	public KickResult(int rc1, int rc2, int rc3, String stdout, String stderr) {
		this.rc = rc1;
		this.outrc = rc2;
		this.errrc = rc3;
		this.stdout = stdout;
		this.stderr = stderr;
		if (this.rc == 0 && stdout != null && stdout.indexOf("Success") > 0) {
			this.success = true;
		}
	}

	/**
	 * 
	 * @param sr
	 */
	@SuppressWarnings("unchecked")
	public KickResult(STAFResult sr) {
		if (sr != null) {
			this.rc = sr.rc;
			Map rMap = (Map) sr.resultObj;
			List subList = (List) rMap.get("fileList");
			if (subList.size() > 0) {
				Map subMap1 = (Map) subList.get(0);
				if (subMap1 != null && subMap1.containsKey("data")) {
					this.outrc = Integer.parseInt((String) subMap1.get("rc"));
					this.stdout = (String) subMap1.get("data");
				}
			}
			if (subList.size() > 1) {
				Map subMap2 = (Map) subList.get(1);
				if (subMap2 != null && subMap2.containsKey("data")) {
					this.errrc = Integer.parseInt((String) subMap2.get("rc"));
					this.stderr = (String) subMap2.get("data");
				}
			}
		}
	}

	/**
	 * 
	 * @param sr
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static KickResult formatResult(STAFResult sr) {
		KickResult kr = null;
		int rc1 = 0;
		int rc2 = 0;
		int rc3 = 0;
		String stdout = null;
		String stderr = null;
		if (sr != null) {
			rc1 = sr.rc;
			Map rMap = (Map) sr.resultObj;
			List subList = (List) rMap.get("fileList");
			if (subList.size() > 0) {
				Map subMap1 = (Map) subList.get(0);
				if (subMap1 != null && subMap1.containsKey("data")) {
					rc2 = Integer.parseInt((String) subMap1.get("rc"));
					stdout = (String) subMap1.get("data");
				}
			}
			if (subList.size() > 1) {
				Map subMap2 = (Map) subList.get(1);
				if (subMap2 != null && subMap2.containsKey("data")) {
					rc3 = Integer.parseInt((String) subMap2.get("rc"));
					stderr = (String) subMap2.get("data");
				}
			}
		}
		kr = new KickResult(rc1, rc2, rc3, stdout, stderr);
		return kr;
	}

	public String toString() {
		return "rc: " + rc + "\nstdout: " + stdout + "\nstderr: " + stderr;
	}

	public int getRc() {
		return rc;
	}

	public void setRc(int rc) {
		this.rc = rc;
	}

	public String getStdout() {
		return stdout;
	}

	public void setStdout(String stdout) {
		this.stdout = stdout;
	}

	public String getStderr() {
		return stderr;
	}

	public void setStderr(String stderr) {
		this.stderr = stderr;
	}

	public int getOutrc() {
		return outrc;
	}

	public void setOutrc(int outrc) {
		this.outrc = outrc;
	}

	public int getErrrc() {
		return errrc;
	}

	public void setErrrc(int errrc) {
		this.errrc = errrc;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
}
